package cn.cordys.common.utils;

import cn.cordys.common.constants.BusinessModuleField;
import cn.cordys.common.dto.OptionDTO;
import cn.cordys.common.util.TimeUtils;
import cn.cordys.crm.opportunity.dto.response.OpportunityListResponse;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class OpportunityFieldUtils {


    public static LinkedHashMap<String, Object> getSystemFieldMap(OpportunityListResponse data, Map<String, List<OptionDTO>> optionMap, Map<String, String> stageConfigMap) {
        LinkedHashMap<String, Object> systemFiledMap = new LinkedHashMap<>();
        systemFiledMap.put("name", data.getName());
        systemFiledMap.put("customerId", data.getCustomerName());
        systemFiledMap.put("amount", data.getAmount());
        systemFiledMap.put("expectedEndTime", TimeUtils.getDataStr(data.getExpectedEndTime()));
        systemFiledMap.put("actualEndTime", TimeUtils.getDataStr(data.getActualEndTime()));
        systemFiledMap.put("failureReason", data.getFailureReason());
        systemFiledMap.put("possible", data.getPossible());
        systemFiledMap.put("products", getProducts(optionMap, data.getProducts()));
        systemFiledMap.put("contactId", data.getContactName());
        systemFiledMap.put("owner", data.getOwnerName());

        systemFiledMap.put("stage", stageConfigMap.get(data.getStage()));
        systemFiledMap.put("followerName", data.getFollowerName());
        systemFiledMap.put("followTime", TimeUtils.getDataTimeStr(data.getFollowTime()));
        systemFiledMap.put("reservedDays", data.getReservedDays());
        systemFiledMap.put("departmentId", data.getDepartmentName());

        systemFiledMap.put("createUser", data.getCreateUserName());
        systemFiledMap.put("createTime", TimeUtils.getDataTimeStr(data.getCreateTime()));
        systemFiledMap.put("updateUser", data.getUpdateUserName());
        systemFiledMap.put("updateTime", TimeUtils.getDataTimeStr(data.getUpdateTime()));


        return systemFiledMap;
    }


    private static Object getProducts(Map<String, List<OptionDTO>> optionMap, List<String> products) {
        List<String> productNames = new ArrayList<>();
        if (optionMap.containsKey(BusinessModuleField.OPPORTUNITY_PRODUCTS.getBusinessKey()) && CollectionUtils.isNotEmpty(products)) {
            Map<String, String> productsMap = optionMap.get(BusinessModuleField.OPPORTUNITY_PRODUCTS.getBusinessKey()).stream().collect(Collectors.toMap(OptionDTO::getId, OptionDTO::getName));
            products.forEach(product -> {
                if (productsMap.containsKey(product)) {
                    productNames.add(productsMap.get(product));
                }
            });
        }
        if (CollectionUtils.isEmpty(productNames)) {
            return StringUtils.EMPTY;
        }
        return StringUtils.join(productNames, ",");
    }
}
